package org.apache.commons.graph.algorithms.connectivity

import org.apache.commons.graph.Graph

/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*   http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied.  See the License for the
* specific language governing permissions and limitations
* under the License.
*/

/**
 * Builder to specify the set of vertices included into a connected component.
 *
 * @param <V> the Graph vertices type
 * @param <E> the Graph edges type
</E></V> */
interface ConnectivityBuilder<V, E> {
    /**
     * Find all the connected components included into the specified graph
     *
     * @return the connectivity algorithm selector.
     */
    fun includingAllVertices(): org.apache.commons.graph.algorithms.connectivity.ConnectivityAlgorithmsSelector<V, E>

    /**
     * Specifies the set of vertices included into a connected component.
     *
     * @param vertices the set of vertices included into a connected component.
     * @return the connectivity algorithm selector.
     */
    fun includingVertices(vararg vertices: V): org.apache.commons.graph.algorithms.connectivity.ConnectivityAlgorithmsSelector<V, E>
}


/**
 * Creates a nw instance of [DefaultConnectivityBuilder]
 * @param graph the graph
 */
class DefaultConnectivityBuilder<V, E>(
    private val graph: Graph<V, E>
) : ConnectivityBuilder<V, E> {
    /**
     * {@inheritDoc}
     */
    override fun includingAllVertices(): ConnectivityAlgorithmsSelector<V, E> {
        return DefaultConnectivityAlgorithmsSelector(graph, graph.vertices)
    }

    /**
     * {@inheritDoc}
     */
    override fun includingVertices(vararg vertices: V): ConnectivityAlgorithmsSelector<V, E> {
//        var vertices = vertices
//        vertices = Assertions.checkNotNull<Array<V>>(
//            vertices,
//            "Graph connectivity cannote be applied on null vertices array, use no-args if you intend specify no vertices"
//        )
        return DefaultConnectivityAlgorithmsSelector(graph, vertices.toList())
    }
}

fun <V, E, G : Graph<V, E>> G.findConnectedComponent(): org.apache.commons.graph.algorithms.connectivity.ConnectivityBuilder<V, E> = DefaultConnectivityBuilder(this)

fun <V, E, G : Graph<V, E>> G.findConnectedComponentAll() = findConnectedComponent().includingAllVertices()

fun <V, E, G : Graph<V, E>> G.findConnectedComponent(vararg vertices: V) = findConnectedComponent().includingVertices(*vertices)